<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;

use App\Repositories\GoodsAttributesRepositories;
use Illuminate\Http\Request;

class GoodsAttributesController extends Controller
{
    public function __construct(GoodsAttributesRepositories $goodsAttributesRepositories)
    {
        $this->goodsAttributesRepositories = $goodsAttributesRepositories;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('admin.goods.attributes.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.goods.attributes.create')->render();
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        try
        {
            $ret = $this->goodsAttributesRepositories->save($request->all());

            if($ret)
            {
                return response()->json([
                    'code'=>0,
                    'msg'=>'数据提交成功！',
                    'url'=>route('admin.goods.attributes')
                ]);
            }
            return response()->json([
                'code'=>-1,
                'msg'=>'数据提交失败！',
            ]);
        }
        catch (\Exception $exception)
        {
            return response()->json([
                'code'=>-2,
                'msg'=>$exception->getMessage(),
                ]);
        }

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $info = $this->goodsAttributesRepositories->find($id);
        return view('admin.goods.attributes.edit',compact('info'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        try
        {
            $ret = $this->goodsAttributesRepositories->update($id,$request->all());

            if($ret)
            {
                return response()->json([
                    'code'=>0,
                    'msg'=>'数据提交成功！',
                    'url'=>route('admin.goods.attributes')
                ]);
            }
            return response()->json([
                'code'=>-1,
                'msg'=>'数据提交失败！',
            ]);
        }
        catch (\Exception $exception)
        {
            return response()->json([
                'code'=>-2,
                'msg'=>$exception->getMessage(),
            ]);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy()
    {
       $ids=\request()->input('ids');
       $ret =$this->goodsAttributesRepositories->delete($ids);
       if($ret)
       {
           return response()->json([
               'code' => 0,
               'msg'   => '删除成功！',
           ]);
       }
        return response()->json(['ok'=>222]);
    }

    public function data(Request $request)
    {
        $data = $request->all();
        $res = $this->goodsAttributesRepositories->search($data);
        $data = [
            'code' => 0,
            'msg'   => '正在请求中...',
            'count' => $res['total'],
            'data'  => $res['data']
        ];
        return response()->json($data);
    }
}
